from . import models
from rest_framework import serializers

'''
    name = models.CharField(max_length=128, verbose_name="电影名称")
    movie_img = models.ImageField(upload_to="courses", max_length=255, verbose_name="封面图片", blank=True, null=True)
    movie_type = models.SmallIntegerField(choices=movie_type, default=0, verbose_name="付费类型")
    # 使用这个字段的原因
    brief = models.TextField(max_length=2048, verbose_name="详情介绍", null=True, blank=True)
    level = models.SmallIntegerField(choices=level_choices, default=0, verbose_name="推荐等级")
    pub_date = models.DateField(verbose_name="发布日期", auto_now_add=True)
    show_time = models.CharField(max_length=32, verbose_name='上映时间')
    attachment_path = models.FileField(upload_to="attachment", max_length=128, verbose_name="电影路径", blank=True, null=True)
    price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="电影价格", default=0)

    # 优化字段
    people = models.IntegerField(verbose_name="观看人数", default=0)

    # 关联字段
    director = models.ForeignKey('Director', on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False,verbose_name="导演")
    performer = models.ManyToManyField("Performer", blank=True, verbose_name="演员",db_constraint=False)
    movie_category = models.ForeignKey("MovieCategory", on_delete=models.SET_NULL, db_constraint=False, null=True, blank=True,verbose_
'''

class MovieModelSerializer(serializers.ModelSerializer):
    # 子序列化的方式
    director = serializers.CharField(source='director.name')
    # performer = serializers.CharField(source='performer')
    class Meta:
        model = models.Movie
        fields=[
            'id',
            'name',
            'movie_img',
            'brief',
            'attachment_path',
            'price',
            'people',
            'course_type_name',
            'level',
            'performer',
            'director',
            'movie_categorys',
            'show_time',
            'movie_type'
        ]

class MovieCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = models.MovieCategory
        fields = ['id', 'name']


class CommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Comment
        fields = ['id', 'username','content','comment_time','movie_title','orders']
        extra_kwargs = {
            'comment_time': {'read_only': True},
            'id': {'read_only': True}
        }

